import { NameSpace } from 'utils/index';
import exceed from 'utils/apimap';
import dayjs from 'dayjs';
import intl from '../../../../../../../locale';

const ns = NameSpace('project-statistics');
export const UPDATE_STATISTICS_DATA = ns('update_statistics_data');
export const DO_PERSONAL_CR_SORT = ns('do_personal_cr_sort');

export function updateStatisticsData(data) {
  return (dispatch) => {
    dispatch({
      type: UPDATE_STATISTICS_DATA,
      data,
    });
  };
}

export function getTotalCommitData() {
  return (dispatch, getState) => {
    updateStatisticsData({
      isLoadingCommit: true,
    })(dispatch);
    const { pathResource } = window.FORCE;
    const { commit_start_time, commit_end_time, commit_branch } = getState().project.projectInsight;

    exceed
      .fetch({
        api: 'project.reports.total_commit',
        params: { projectId: pathResource.id },
        data: {
          start_date: dayjs(commit_start_time).format('YYYYMMDD'),
          end_date: dayjs(commit_end_time).format('YYYYMMDD'),
          branch:
            commit_branch ===
            intl.get({ id: 'code-assets.projectInsight.actions.All', defaultMessage: '全部' })
              ? null
              : commit_branch,
        },
      })
      .then((result) => {
        updateStatisticsData({
          isLoadingCommit: false,
          total_commit: result,
        })(dispatch);
      })
      .fail(() => {
        updateStatisticsData({
          isLoadingCommit: false,
        })(dispatch);
      });
  };
}

export function getDetailCommitData() {
  return (dispatch, getState) => {
    const { pathResource } = window.FORCE;
    const { commit_start_time, commit_end_time, commit_branch } = getState().project.projectInsight;

    exceed
      .fetch({
        api: 'project.reports.detail_commit',
        params: { projectId: pathResource.id },
        data: {
          start_date: dayjs(commit_start_time).format('YYYYMMDD'),
          end_date: dayjs(commit_end_time).format('YYYYMMDD'),
          branch:
            commit_branch ===
            intl.get({ id: 'code-assets.projectInsight.actions.All', defaultMessage: '全部' })
              ? null
              : commit_branch,
        },
      })
      .then((result) => {
        const data = result.slice(1);
        updateStatisticsData({
          isLoadingCommit: false,
          detail_commit: data,
        })(dispatch);
      });
  };
}

export function getTotalMrData() {
  return (dispatch, getState) => {
    updateStatisticsData({
      isLoadingTotalMr: true,
    })(dispatch);
    const { pathResource } = window.FORCE;
    const { mr_start_time, mr_end_time } = getState().project.projectInsight;

    exceed
      .fetch({
        api: 'project.reports.total_review_commit',
        params: { projectId: pathResource.id },
        data: {
          start_date: dayjs(mr_start_time).format('YYYYMMDD'),
          end_date: dayjs(mr_end_time).format('YYYYMMDD'),
        },
      })
      .then((result) => {
        updateStatisticsData({
          isLoadingTotalMr: false,
          total_mr: result,
        })(dispatch);
      })
      .fail(() => {
        updateStatisticsData({
          isLoadingTotalMr: false,
        })(dispatch);
      });
  };
}

export function getDetailMrData() {
  return (dispatch, getState) => {
    updateStatisticsData({
      isLoadingDetailMr: true,
    })(dispatch);
    const { pathResource } = window.FORCE;
    const { mr_start_time, mr_end_time } = getState().project.projectInsight;

    exceed
      .fetch({
        api: 'project.reports.personal_review_detail',
        params: { projectId: pathResource.id },
        data: {
          start_date: dayjs(mr_start_time).format('YYYYMMDD'),
          end_date: dayjs(mr_end_time).format('YYYYMMDD'),
        },
      })
      .then((result) => {
        const data = result.slice(1);
        updateStatisticsData({
          isLoadingDetailMr: false,
          detail_mr: data.filter((v) => {
            if (v.user_info && v.user_info.name === 'unknown') {
              if (
                v.participate_review_count ||
                v.review_commits_count ||
                v.review_latency ||
                v.review_lines_count ||
                v.submit_review_count ||
                v.total_commits_count ||
                v.total_lines_count
              ) {
                return true;
              } else {
                return false;
              }
            }
            return true;
          }),
        })(dispatch);
      })
      .fail(() => {
        updateStatisticsData({
          isLoadingDetailMr: false,
        })(dispatch);
      });
  };
}

export function getCommentSummaryData() {
  return (dispatch, getState) => {
    updateStatisticsData({
      isLoadingComment: true,
    })(dispatch);
    const { pathResource } = window.FORCE;
    const { mr_start_time, mr_end_time } = getState().project.projectInsight;

    exceed
      .fetch({
        api: 'project.reports.review_comment_summary',
        params: { projectId: pathResource.id },
        data: {
          start_date: dayjs(mr_start_time).format('YYYYMMDD'),
          end_date: dayjs(mr_end_time).format('YYYYMMDD'),
        },
      })
      .then((result) => {
        updateStatisticsData({
          isLoadingComment: false,
          comment_summary: result,
        })(dispatch);
      })
      .fail(() => {
        updateStatisticsData({
          isLoadingComment: false,
        })(dispatch);
      });
  };
}

export function doPersonalCRSort(data) {
  return {
    type: DO_PERSONAL_CR_SORT,
    data,
  };
}
